#pragma once

#include <assert.h>
//#include "RerverseIterator.h"


namespace xyx
{
	template<class T>
	struct list_node
	{
		//成员变量k
		list_node<T>* _prev;
		list_node<T>* _next;
		T _data;

		list_node(const T& x = T())
			:_prev(nullptr)
			, _next(nullptr)
			, _data(x)
		{
		}

		////完美转发
		//template<class Ty>
		//list_node(Ty&& x)
		//	:_prev(nullptr)
		//	, _next(nullptr)
		//	, _data(forward<Ty>(x))
		//{
		//}

		template< class ...Args>
		list_node(Args... args)
			:_prev(nullptr)
			, _next(nullptr)
			, _data(forward<Args>(args)...)
		{
		}



		list_node(T&& x)
			: _prev(nullptr)
			, _next(nullptr)
			, _data(forward<T>(x))
		{
		}
		// 


	};

	//typedef __list_iterator<T, T&, T*> iterator;
	//typedef __list_iterator<T, const T&, const T*> const_iterator;

	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> Self;
		//成员变量
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{}

		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		Self operator++(int)
		{
			Self tmp(*this);
			_node = _node->_next;
			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		Self operator--(int)
		{
			Self tmp(*this);
			_node = _node->_prev;
			return tmp;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		Ref operator*()
		{
			return _node->_data;
		}

		bool operator!=(const Self& it)const
		{
			return _node != it._node;
		}


		bool operator==(const Self& it)const
		{
			return _node == it._node;
		}
	};

	//template<class T>
	//struct __list_const_iterator
	//{
	//	typedef list_node<T> Node;

	//	//成员变量
	//	Node* _node;

	//	__list_const_iterator(Node* node)
	//		:_node(node)
	//	{}

	//	__list_const_iterator<T>& operator++()
	//	{
	//		_node = _node->_next;
	//		return *this;
	//	}

	//	__list_const_iterator<T>& operator++(int)
	//	{
	//		__list_const_iterator<T> tmp(*this);
	//		_node = _node->_next;
	//		return tmp;
	//	}

	//	__list_const_iterator<T>& operator--()
	//	{
	//		_node = _node->_prev;
	//		return *this;
	//	}

	//	__list_const_iterator<T>& operator--(int)
	//	{
	//		__list_const_iterator tmp(*this);
	//		_node = _node->_prev;
	//		return tmp;
	//	}

	//	const T& operator*()
	//	{
	//		return _node->_data;
	//	}

	//	bool operator!=(const __list_const_iterator<T>& it)
	//	{
	//		return _node != it._node;
	//	}


	//	bool operator==(const __list_const_iterator<T>& it)
	//	{
	//		return _node == it._node;
	//	}
	//};

	template<class T>
	class list
	{
		typedef list_node<T> Node;


	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		//typedef RerverseIterator<iterator, T&, T*> reverse_iterator;
		//typedef RerverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;

		/*reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}*/

		iterator begin()
		{
			return _head->_next;//单参数构造支持隐式类型转化
			//return iterator(_head->next);
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin() const
		{
			return _head->_next;//单参数构造支持隐式类型转化
			//return iterator(_head->next);
		}

		const_iterator end() const
		{
			return _head;
		}

		void empty_init()
		{
			_head = new Node(T());
			_head->_prev = _head;
			_head->_next = _head;
			_size = 0;
		}

		list(const list<T>& lt)
		{
			empty_init();

			for (auto& e : lt)
				push_back();
		}

		list()
		{
			empty_init();
		}


		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}


		//pos位置之前插入
		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			Node* newnode = new Node(x);

			newnode->_prev = prev;
			prev->_next = newnode;

			newnode->_next = cur;
			cur->_prev = newnode;

			++_size;

			return newnode;
		}

		iterator insert(iterator pos, T&& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			Node* newnode = new Node(forward<T>(x));

			newnode->_prev = prev;
			prev->_next = newnode;

			newnode->_next = cur;
			cur->_prev = newnode;

			++_size;

			return newnode;
		}

		iterator erase(iterator pos)
		{
			assert(pos != end());

			Node* cur = pos._node;;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;

			delete cur;

			--_size;

			return next;

		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		template<class ...Args>
		iterator emplace(iterator pos, Args&&... args)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(forward<Args>(args)...);//

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;

			//return iterator(newnode);
			return newnode;
		}
		template<class ...Args>
		void emplace_back(Args&&... args)
		{
			emplace(end(), forward<Args>(args)...);
		}

		void push_back(T&& x)
		{
			insert(end(), forward<T>(x));
		}

		void pop_back()
		{
			erase(--end());
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		~list()
		{
			clear();

			delete _head;
			_head = nullptr;
		}


		void clear()
		{
			iterator it = begin();
			while (it != end())
				it = erase(it);

			_size = 0;
		}

		void pop_front()
		{
			erase(begin());
		}

		size_t size()
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

	void Print(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			// (*it) += 1;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_01()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);

		Print(lt);

	}

	void test_list3()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_front(5);
		lt.push_front(6);
		lt.push_front(7);
		lt.push_front(8);
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		lt.pop_front();
		lt.pop_back();

		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		lt.clear();
		lt.push_back(10);
		lt.push_back(20);
		lt.push_back(30);
		lt.push_back(40);
		for (auto e : lt)
		{
			cout << e << " ";
		}
		cout << endl;

		cout << lt.size() << endl;
	}

	void test_list4()
	{
		xyx::list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);

		//xyx::list<int>::reverse_iterator rit = lt.rbegin();

		//while (rit != lt.rend())
		//{
		//	cout << *rit << " ";
		//	++rit;
		//}
	}
}

